package org.argeproje.resim.proc;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import org.argeproje.resim.proc.data.*;

/*
Definition
  Finds histogram of image. 
Inputs
  ImageDA
Parameters
  -
Outputs
  HistogramDA
 */
public class HistogramPR extends IntensityTransformPR {
    protected PlanarImage _histImage;
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
		connRule.addOutType(HistogramDA.class);
	}
    protected PlanarImage getHistImage(){
    	return _histImage;
    }
    protected void setHistImage(PlanarImage histImage){
    	_histImage = histImage;
    }
	public Data process() {
		PlanarImage im = getInputAsPlanarImage();
		int nBands = im.getSampleModel().getNumBands();
		int [] bins = new int [nBands];
		double [] low = new double [nBands];
		double [] high = new double [nBands];
		for (int i = 0; i < nBands; i++){
			bins[i] = 256;
			low[i] = 0.0;
			high[i] = 255.0;
		}
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(im);
		pb.add(null);
		pb.add(1);
		pb.add(1);
		pb.add(bins);
		pb.add(low);
		pb.add(high);
		PlanarImage histImage = (PlanarImage)JAI.create("histogram", pb, null);
		setHistImage(histImage);
		Histogram hist = (Histogram) histImage.getProperty("histogram");
		HistogramDA outDA = new HistogramDA(hist);
		outDA.setData(hist);
		setOutput(outDA);
		return getOutput();
	}
}
